#!/bin/bash

function info() {
echo Usage: `basename $0` '[-w win_len] bed'
exit 1
}

while getopts  ":p:f:w:" opt; do
	case  $opt  in
		p) out_prefix=$OPTARG;;
		f) suffix=$OPTARG;;
        w) win_len=$OPTARG;;
		*) info;;
	esac
done
shift $(($OPTIND - 1))


if [ $# -lt 1 ]; then info; fi

. $var


bed=$data/bed/b37.chr1y.bed
win_len=${win_len:=1000}

if test ! -s $bed.split_$win_len; then
    bedtools makewindows -w $win_len -b $bed > $bed.split_$win_len
fi

tes=$1

bedtools sort -i $tes > $tes.sort

bedtools cluster -i $tes.sort -d $win_len > $tes.cluster

bedtools intersect -a $tes -b $bed.split_$win_len -wb|awk 'BEGIN{OFS="\t"}{print $1,$2,$3,$4,$5 "_" $6 "_" $7}' > $tes.intersect

cut -f4-5 $tes.intersect > $tes.intersect.id_change_$win_len
cut -f4-5 $tes.cluster |sed 's/$/_/'> $tes.cluster.id_change_$win_len

